﻿using EasyNet.Componets.Core.Serialization;
using EasyNet.Componets.Core.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core
{
    /// <summary>
    /// 对象扩展类
    /// </summary>
    public static class ObjectExtention
    {
        /// <summary>
        /// 转化成字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <returns></returns>
        [DebuggerStepThrough, DebuggerHidden]
        public static string ToStringOrEmpty(this object sender)
        {
            if (sender == null)
            {
                return string.Empty;
            }
            return sender.ToString();
        }

        #region 序列化

        /// <summary>
        /// 将对象序列化成Json格式字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>Json格式字符串</returns>
        public static string ToJsonString(this object obj)
        {
            return SerializationUtil.Json.Serialize(obj);
        }

        /// <summary>
        /// 将对象序列化成Xml格式字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="rootNodeName">根节点名称</param>
        /// <returns>Xml格式字符串</returns>
        public static string ToXmlString(this object obj, string rootNodeName = "xml")
        {
            return SerializationUtil.Xml.Serialize(obj, rootNodeName);

        }

        /// <summary>
        /// 将对象序列化成Form表单请求格式字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>Form表单请求格式字符串</returns>
        public static string ToHttpQuryString(this object obj)
        {
            return SerializationUtil.HttpQueryString.Serialize(obj);

        }
        #endregion


    }
}
